#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
快速设置服务为自动启动
"""

import json
import sys
from pathlib import Path

# 项目根目录
project_root = Path(__file__).parent.parent
config_file = project_root / "config.json"

def update_config_to_auto():
    """更新配置文件，设置服务为自动启动"""
    print("========================================")
    print("设置WMS服务为自动启动")
    print("========================================")
    print()
    
    try:
        # 读取现有配置
        if config_file.exists():
            with open(config_file, 'r', encoding='utf-8') as f:
                config = json.load(f)
            print("✅ 读取现有配置文件")
        else:
            config = {}
            print("📝 创建新配置文件")
        
        # 更新启动类型
        old_type = config.get("service_startup_type", "未设置")
        config["service_startup_type"] = "auto"
        
        # 确保其他必要配置存在
        default_config = {
            "api_host": "127.0.0.1",
            "api_port": 58500,
            "cors_origins": ["*"],
            "database_path": "data/wmssimple.db",
            "log_level": "INFO"
        }
        
        for key, value in default_config.items():
            if key not in config:
                config[key] = value
        
        # 保存配置
        config_file.parent.mkdir(parents=True, exist_ok=True)
        with open(config_file, 'w', encoding='utf-8') as f:
            json.dump(config, f, indent=2, ensure_ascii=False)
        
        print(f"✅ 启动类型已更新: {old_type} → auto (自动)")
        print(f"✅ 配置文件已保存: {config_file}")
        print()
        print("现在安装的服务将设置为开机自启动！")
        
        return True
        
    except Exception as e:
        print(f"❌ 更新配置失败: {e}")
        return False

if __name__ == "__main__":
    success = update_config_to_auto()
    if not success:
        sys.exit(1) 